package com.tbq.tbqj_ggl.activity

import android.animation.ObjectAnimator
import android.graphics.drawable.AnimationDrawable
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.tbq.sdk.L
import com.tbq.sdk.TBQ
import com.tbq.tbqj_ggl.R
import com.tbq.tbqj_ggl.model.Prizes
import com.tbq.tbqj_ggl.utils.RandomUtil
import com.tbq.tbqj_ggl.view.ScratchImageView
import com.tbq.tbqj_ggl.view.ScratchTextView
import pw.hais.http.Http
import java.util.*

class ScratchHorseRacingGameActivity : ScrachGameBaseActivity(if (TBQ.get("KEY_IS_MOBILE") == "1") R.layout.mobile_horse_racing_game else
    R.layout.activity_horse_racing_game), ScratchTextView.OnScratchListent, ScratchImageView.OnScratchListent, View.OnClickListener {
    override fun onClick(p0: View?) {
        if (jiangpinImage.isScratch || miange1.isScratch || miange2.isScratch || miange3.isScratch || miange4.isScratch || miange5.isScratch || miange6.isScratch || miange7.isScratch || miange8.isScratch || miange9.isScratch || miange10.isScratch || miange11.isScratch || miange12.isScratch) {
            jiangpinImage.setComplete(true)
            miange1.isComplete = true
            miange2.isComplete = true
            miange3.isComplete = true
            miange4.isComplete = true
            miange5.isComplete = true
            miange6.isComplete = true
            miange7.isComplete = true
            miange8.isComplete = true
            miange9.isComplete = true
            miange10.isComplete = true
            miange11.isComplete = true
            miange12.isComplete = true
        } else {
            L.showShort("要先刮动涂层才可以直接 兑奖 哟！")
        }

    }

    override fun onScratchItem(view: ScratchImageView?) {
        findViewById(R.id.text_start).setBackgroundDrawable(resources.getDrawable(R.drawable.scratch_game_btzlyc))
    }

    override fun onScratchItem(view: ScratchTextView?) {
        findViewById(R.id.text_start).setBackgroundDrawable(resources.getDrawable(R.drawable.scratch_game_btzlyc))
    }

    private val text_dfdjuli by lazy { findViewById(R.id.id_horse_racing_text_dfdjuli) as TextView }
    private val text_wdjuli by lazy { findViewById(R.id.id_horse_racing_text_wdjuli) as TextView }
    private val wdmaImage by lazy { findViewById(R.id.id_horse_racing_image_wdma) as ImageView }
    private val dfdmaImage by lazy { findViewById(R.id.id_horse_racing_image_dfdma) as ImageView }
    private val duifangdixianImage by lazy { findViewById(R.id.id_horse_racing_image_duifangdixian) as ImageView }
    private val wodixianImage by lazy { findViewById(R.id.id_horse_racing_image_wodixian) as ImageView }
    private var wodeanimaition: AnimationDrawable? = null
    private var duifanganimaition: AnimationDrawable? = null
    private val miange1 by lazy { findViewById(R.id.id_horse_racing_scratchimage_1) as ScratchTextView }
    private val miange2 by lazy { findViewById(R.id.id_horse_racing_scratchimage_2) as ScratchTextView }
    private val miange3 by lazy { findViewById(R.id.id_horse_racing_scratchimage_3) as ScratchTextView }
    private val miange4 by lazy { findViewById(R.id.id_horse_racing_scratchimage_4) as ScratchTextView }
    private val miange5 by lazy { findViewById(R.id.id_horse_racing_scratchimage_5) as ScratchTextView }
    private val miange6 by lazy { findViewById(R.id.id_horse_racing_scratchimage_6) as ScratchTextView }
    private val miange7 by lazy { findViewById(R.id.id_horse_racing_scratchimage_7) as ScratchTextView }
    private val miange8 by lazy { findViewById(R.id.id_horse_racing_scratchimage_8) as ScratchTextView }
    private val miange9 by lazy { findViewById(R.id.id_horse_racing_scratchimage_9) as ScratchTextView }
    private val miange10 by lazy { findViewById(R.id.id_horse_racing_scratchimage_10) as ScratchTextView }
    private val miange11 by lazy { findViewById(R.id.id_horse_racing_scratchimage_11) as ScratchTextView }
    private val miange12 by lazy { findViewById(R.id.id_horse_racing_scratchimage_12) as ScratchTextView }
    private val jiangpinImage by lazy { findViewById(R.id.id_horse_racing_image_jiangpin) as ScratchImageView }

    private var woDeMaNum: MutableList<Int> = ArrayList()
    private var duiFangMaNum: MutableList<Int> = ArrayList()
    private var isWin: Boolean = false
    private var xianWidthBaiFenZhiYi: Int = 0
    private val xianWidth: Int = 0
    private var oneWidth = 0
    private var towWidth = 0
    private var wdweizhi: Float = 0.toFloat()
    private var dfdweizhi: Float = 0.toFloat()
    private var wdJuLi_x: Float = 0.toFloat()
    private var dfdJuLi_x: Float = 0.toFloat()

    private var jiuweiyi_X = 0f
    private var jisuanShuLiang = 0

    private var wdjuli = 0
    private var dfdjuli = 0
    private var a = 0
    private var b = 0

    override fun initDataAndView() {
        jiangpinImage.setOnScratchListent(this)

        miange1.setAutoCompleteNumber(80)
        miange2.setAutoCompleteNumber(80)
        miange3.setAutoCompleteNumber(80)
        miange4.setAutoCompleteNumber(80)
        miange5.setAutoCompleteNumber(80)
        miange6.setAutoCompleteNumber(80)

        miange7.setAutoCompleteNumber(80)
        miange8.setAutoCompleteNumber(80)
        miange9.setAutoCompleteNumber(80)
        miange10.setAutoCompleteNumber(80)
        miange11.setAutoCompleteNumber(80)
        miange12.setAutoCompleteNumber(80)

        miange1.setOnScratchListent(this)
        miange2.setOnScratchListent(this)
        miange3.setOnScratchListent(this)
        miange4.setOnScratchListent(this)
        miange5.setOnScratchListent(this)
        miange6.setOnScratchListent(this)

        miange7.setOnScratchListent(this)
        miange8.setOnScratchListent(this)
        miange9.setOnScratchListent(this)
        miange10.setOnScratchListent(this)
        miange11.setOnScratchListent(this)
        miange12.setOnScratchListent(this)

        for (i in 0..5) {
            woDeMaNum.add(0)
            duiFangMaNum.add(0)
        }

        setPicture()
        wdweizhi = wdmaImage.x
        dfdweizhi = dfdmaImage.x
        wdJuLi_x = text_wdjuli.x
        dfdJuLi_x = text_dfdjuli.x

        xianWidthBaiFenZhiYi = 5

        (findViewById(R.id.text_message) as TextView).text = "1.刮奖可以选择多种面额,面额越大奖品越丰富。\n" +
                "2.刮开涂层，一共六轮，我的赛马和别人赛马对比哪匹马跑的更远。只要我的赛马比别人的赛马跑的远，就可以获得奖品区奖品。\n" +
                "3.中奖后选择领取奖品方式。\n" +
                "4.\"再来一次\"即放弃本局刮奖,并消耗相应面额金币进行下一轮刮奖。"

        findViewById(R.id.text_start).setOnClickListener(this)
    }

    override fun initDataToStartGame(challengePrize: Prizes, isWin: Boolean) {
        super.initDataToStartGame(challengePrize, isWin)

        //还原数据及清空
        woDeMaNum.clear()
        duiFangMaNum.clear()
        wdjuli = 0
        dfdjuli = 0
        oneWidth = 0
        towWidth = 0
        dfdjuli = 0
        wdjuli = 0
        a = 0
        b = 0
        text_dfdjuli.visibility = View.INVISIBLE
        text_wdjuli.visibility = View.INVISIBLE
        //wdmaImage.setX(wdweizhi + 100);
        // dfdmaImage.setX(dfdweizhi + 100);
        jisuanShuLiang = 0
        ObjectAnimator.ofFloat(wdmaImage, View.TRANSLATION_X, 0f, wdweizhi).setDuration(500).start()
        ObjectAnimator.ofFloat(dfdmaImage, View.TRANSLATION_X, 0f, dfdweizhi).setDuration(500).start()
        ObjectAnimator.ofFloat(text_dfdjuli, View.TRANSLATION_X, 0f, dfdJuLi_x).setDuration(500).start()
        ObjectAnimator.ofFloat(text_wdjuli, View.TRANSLATION_X, 0f, wdJuLi_x).setDuration(500).start()
        //开始游戏必须的赋值
        this.isWin = isWin
        jiangpinImage.resteData()
        Http.displayImageAutoSize(jiangpinImage, SERVER_URL + challengePrize.Icon)
        miange1.resteData()
        miange2.resteData()
        miange3.resteData()
        miange4.resteData()
        miange5.resteData()
        miange6.resteData()
        miange7.resteData()
        miange8.resteData()
        miange9.resteData()
        miange10.resteData()
        miange11.resteData()
        miange12.resteData()
        woDeMaNum = randomNumber(null, false)
        duiFangMaNum = randomNumber(woDeMaNum, isWin)
        miange1.text = woDeMaNum[0].toString() + ""
        miange2.text = woDeMaNum[1].toString() + ""
        miange3.text = woDeMaNum[2].toString() + ""
        miange4.text = woDeMaNum[3].toString() + ""
        miange5.text = woDeMaNum[4].toString() + ""
        miange6.text = woDeMaNum[5].toString() + ""
        miange7.text = duiFangMaNum[0].toString() + ""
        miange8.text = duiFangMaNum[1].toString() + ""
        miange9.text = duiFangMaNum[2].toString() + ""
        miange10.text = duiFangMaNum[3].toString() + ""
        miange11.text = duiFangMaNum[4].toString() + ""
        miange12.text = duiFangMaNum[5].toString() + ""

    }


    override fun onScratchFull(view: ScratchImageView) {
        when (view.id) {
            R.id.id_horse_racing_image_jiangpin -> maMove(0, dfdmaImage, 0)
        }
    }

    override fun onScratchFull(view: ScratchTextView) {

        when (view.id) {
            R.id.id_horse_racing_scratchimage_1 -> {
                a += 1
                maMove(woDeMaNum[0], wdmaImage, 1)
            }
            R.id.id_horse_racing_scratchimage_2 -> {
                a += 1
                maMove(woDeMaNum[1], wdmaImage, 1)
            }
            R.id.id_horse_racing_scratchimage_3 -> {
                a += 1
                maMove(woDeMaNum[2], wdmaImage, 1)
            }
            R.id.id_horse_racing_scratchimage_4 -> {
                a += 1
                maMove(woDeMaNum[3], wdmaImage, 1)
            }
            R.id.id_horse_racing_scratchimage_5 -> {
                a += 1
                maMove(woDeMaNum[4], wdmaImage, 1)
            }
            R.id.id_horse_racing_scratchimage_6 -> {
                a += 1
                maMove(woDeMaNum[5], wdmaImage, 1)
            }
            R.id.id_horse_racing_scratchimage_7 -> {
                b += 1
                maMove(duiFangMaNum[0], dfdmaImage, 2)
            }
            R.id.id_horse_racing_scratchimage_8 -> {
                b += 1
                maMove(duiFangMaNum[1], dfdmaImage, 2)
            }
            R.id.id_horse_racing_scratchimage_9 -> {
                b += 1
                maMove(duiFangMaNum[2], dfdmaImage, 2)
            }
            R.id.id_horse_racing_scratchimage_10 -> {
                b += 1
                maMove(duiFangMaNum[3], dfdmaImage, 2)
            }
            R.id.id_horse_racing_scratchimage_11 -> {
                b += 1
                maMove(duiFangMaNum[4], dfdmaImage, 2)
            }
            R.id.id_horse_racing_scratchimage_12 -> {
                b += 1
                maMove(duiFangMaNum[5], dfdmaImage, 2)
            }
        }
    }

    /**
     * 马移动方法

     * @param i      刮图所得的数字
     * *
     * @param mImage 要移动的图片控件
     * *
     * @param type   要移动的图片是那一张1或者2
     */

    private fun maMove(i: Int, mImage: ImageView, type: Int) {
        val wdweiht_x = wdmaImage.x
        val dfdweiht_x = dfdmaImage.x
        val text_dfd = text_dfdjuli.x
        val text_wd = text_wdjuli.x
        if (isPlayGameing) {
            if (type == 1) {
                oneWidth = i * xianWidthBaiFenZhiYi
                wdmaImage.x = wdweiht_x + oneWidth
                text_wdjuli.x = text_wd + oneWidth
                wdjuli += i
                text_wdjuli.visibility = View.VISIBLE
                text_wdjuli.text = "我跑了" + wdjuli + "米"
            } else if (type == 2) {
                towWidth = i * xianWidthBaiFenZhiYi
                dfdmaImage.x = dfdweiht_x + towWidth
                text_dfdjuli.x = text_dfd + towWidth
                dfdjuli += i
                text_dfdjuli.visibility = View.VISIBLE
                text_dfdjuli.text = "我跑了" + dfdjuli + "米"
                //  AnimationUtils.translate(mImage, 0, towWidth, 0, 0, 1, 1000, true);
            }
            jisuanShuLiang += 1
            if (jisuanShuLiang == 13) {
                findViewById(R.id.text_start).setBackgroundDrawable(resources.getDrawable(R.drawable.scratch_game_btzlyc_hs))
                showGameResultWindows(isWin)
            }
        }
    }

    /**
     * @param woDeMaNum 我的马的数字总和，获取我的马传null
     * *
     * @param isWin     是否赢
     * *
     * @return
     */
    private fun randomNumber(woDeMaNum: List<Int>?, isWin: Boolean): MutableList<Int> {
        var myNumberCount = 0
        if (woDeMaNum != null) {
            for (i in woDeMaNum) {
                myNumberCount += i
            }
        }
        val numbers = ArrayList<Int>()
        var zongshu = 0
        while (numbers.size < 6) {
            val winNumber = RandomUtil.getRandom(10)     //随机一个数
            if (myNumberCount == 0) {
                numbers.add(winNumber)
            } else if (isWin && myNumberCount - winNumber > 0) {  //如果赢，
                myNumberCount -= winNumber
                numbers.add(winNumber)
            } else if (!isWin) {
                zongshu += winNumber
                numbers.add(winNumber)
                if (numbers.size == 6 && myNumberCount - zongshu >= 0) {
                    numbers.clear()
                    zongshu = 0
                }
            }

        }
        return numbers
    }

    /**
     * 设置马跑动
     */
    private fun setPicture() {
        wdmaImage.setBackgroundResource(R.drawable.animation_list)
        dfdmaImage.setBackgroundResource(R.drawable.animation_list)
        wodeanimaition = wdmaImage.background as AnimationDrawable
        duifanganimaition = dfdmaImage.background as AnimationDrawable
        animaition(wodeanimaition!!)
        animaition(duifanganimaition!!)
    }

    /**
     * 图片滚动

     * @param animation
     */
    private fun animaition(animation: AnimationDrawable) {
        animation.isOneShot = false
        if (animation.isRunning)
        //是否正在运行？
        {
            animation.stop()//停止
        }
        animation.start()//启动
    }
}
